<template>
	<view class="bf h100v">
		<!-- 搜索区域 -->
		<view class="search-box">
			<view class="search-bar f-s-c">
				<text class="iconfont iconss f28"></text>
				<u-input class="search-input" v-model="query.keyword" placeholder="请输入订单号或流水号" confirm-type="search"
					@confirm="refresh" />
			</view>
		</view>

		<!-- 时间筛选 -->
		<scroll-view class="time-filter" scroll-x>
			<block v-for="(v,i) in sarr" :key='i'>
				<view @click="clickSarr(i)" class="time-item" :class="{active:v.active}">
					{{v.name}}
				</view>
			</block>
		</scroll-view>

		<!-- 订单列表 -->
		<scroll-view class="order-list" scroll-y @scrolltolower="nextPage">
			<view  class="order-item">
				<view class="order-header">
					<text>订单编号：111588662652</text>
					
				</view>
				<view class="order-body">
					<view class="info-row">
						<text>下单时间：11111</text>
					</view>
					<view class="info-row">
						<text>收货人：11111</text>
						<text>电话：1111</text>
					</view>
					<view class="info-row">
						<text>配送地址：11111111111111111111111</text>
					</view>
				</view>
			</view>
			<view  class="order-item">
				<view class="order-header">
					<text>订单编号：111588662652</text>
					
				</view>
				<view class="order-body">
					<view class="info-row">
						<text>下单时间：11111</text>
					</view>
					<view class="info-row">
						<text>收货人：11111</text>
						<text>电话：1111</text>
					</view>
					<view class="info-row">
						<text>配送地址：11111111111111111111111</text>
					</view>
				</view>
			</view>

			<u-empty v-if="showEmpty" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
			<u-loadmore v-else :status="noMore ? 'nomore' : 'loading'" />
		</scroll-view>

		<!-- 日期选择 -->
		<u-calendar v-model="showrl" mode="range" :btnstyle="{ background: '#FE9367' }" range-color="#FE9367"
			@change="changeDate" active-bg-color='#FE9367'></u-calendar>
	</view>
</template>

<script>
	import utils from '@/common/utils.js';
	import {
		utilMixins
	} from "@/common/util-mixins.js"
	export default {
		mixins: [utilMixins],
		data() {
			return {
				showrl: false,
				noMore: false,
				query: {
					keyword: '',
					page: 1,
					size: 10, // 修改为size
					status: 0,
					timeType: 1,
					startTime: '',
					endTime: ''
				},
				data: [{
						id: '2023010112345678',
						add_time: 1672531199000,
						recipient_name: '张三',
						recipient_phone: '13800138000',
						delivery_address: '北京市海淀区中关村大街1号',
						status: 1
					},
					{
						id: '2023010112345679',
						add_time: 1672531199000,
						recipient_name: '李四',
						recipient_phone: '13800138000',
						delivery_address: '北京市海淀区中关村大街1号',
						status: 1,
					}
				],
				sarr: [{
						name: '今天',
						active: true,
						timeType: 1
					},
					{
						name: '近七天',
						active: false,
						timeType: 2
					},
					{
						name: '近十五天',
						active: false,
						timeType: 3
					},
					{
						name: '具体时间',
						active: false,
						timeType: 4
					}
				],
				isLoaded: false // 新增加载状态
			}
		},
		computed: {
			showEmpty() {
				return this.data.length === 0 && this.isLoaded
			}
		},
		onLoad() {
			this.refresh()
		},
		methods: {
			getStatusText(status) {
				const statusMap = {
					'1': '进行中',
				};
				return statusMap[status] || '未知状态';
			},
			statusColor(status) {
				return {
					'1': 'status-processing',
				} [status];
			},
			async refresh() {
				this.query.page = 1;
				this.data = [];
				this.noMore = false;
				await this.fetchData(true);
			},
			async nextPage() {
				if (this.noMore) return;
				this.query.page++;
				await this.fetchData();
			},
			async fetchData(isRefresh = false) {
				try {
					const {
						data: res
					} = await this.util.request({
						url: this.api.ddcz,
						data: this.query
					});

					if (res.code === 1) {
						const newData = res.data || [];

						// 判断是否还有更多数据
						this.noMore = newData.length < this.query.size;

						if (isRefresh) {
							this.data = newData;
						} else {
							this.data = [...this.data, ...newData];
						}
					}
					this.isLoaded = true;
				} catch (e) {
					this.util.message(e.msg, 3);
				}
			},
			clickSarr(index) {
				this.sarr.forEach((item, i) => {
					item.active = i === index
					if (i === index) this.query.timeType = item.timeType
				})
				if (this.query.timeType === 4) {
					this.showrl = true
				} else {
					this.refresh()
				}
			},
			changeDate(e) {
				this.query.startTime = e.startDate
				this.query.endTime = e.endDate
				this.refresh()
			}
		}
	}
</script>

<style scoped lang="scss">
	.search-box {
		padding: 20rpx 30rpx;
		background: #fff;

		.search-bar {
			height: 80rpx;
			background: #f5f5f5;
			border-radius: 12rpx;
			padding: 0 25rpx;

			.search-input {
				flex: 1;
				margin-left: 15rpx;
				font-size: 28rpx;
			}
		}
	}

	.time-filter {
		white-space: nowrap;
		padding: 20rpx 30rpx;
		background: #fff;

		.time-item {
			display: inline-block;
			padding: 12rpx 30rpx;
			margin-right: 20rpx;
			border-radius: 12rpx;
			background: #f5f5f5;
			color: #666;
			font-size: 26rpx;

			&.active {
				background: #FE9367;
				color: #fff;
			}
		}
	}

	.order-list {
		height: calc(100vh - 240rpx);
		padding: 0 30rpx;

		.order-item {
			background: #fff;
			border-radius: 12rpx;
			margin: 20rpx 0;
			padding: 24rpx;

			.order-header {
				display: flex;
				justify-content: space-between;
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #eee;

				text:first-child {
					color: #333;
					font-weight: 500;
				}

				[class^="status-"] {
					font-size: 24rpx;
					padding: 4rpx 12rpx;
					border-radius: 6rpx;
				}

				.status-processing {
					color: #FF9F00;
					background: #FFF3E0;
				}

				.status-completed {
					color: #FE9367;
					background: #E8F8EE;
				}

				.status-canceled {
					color: #999;
					background: #f5f5f5;
				}
			}

			.order-body {
				padding-top: 20rpx;

				.info-row {
					display: flex;
					justify-content: space-between;
					margin: 12rpx 0;
					font-size: 26rpx;
					color: #666;
				}
			}
		}
	}
</style>